package hu.blok1.veilingsite.server;

import hu.blok1.veilingsite.client.data.AccountServiceAdmin;
import hu.blok1.veilingsite.shared.Product;
import hu.blok1.veilingsite.shared.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class ServiceAdminImpl extends RemoteServiceServlet implements
		AccountServiceAdmin {

	private static final long serialVersionUID = 1L;
	
	@Override
	public void ChangeUserStatus(String email, String newstatus) {
		DBConnector.SendStatement("UPDATE users SET status='" + newstatus
				+ "'WHERE email='" + email + "'");
		DBConnector.CloseAll();
	}

	@Override
	public String GetUserStatus(String email) {
		String status = "no status found";
		ResultSet rs = DBConnector
				.GetResultSet("SELECT status FROM users WHERE email ='"
						+ email + "'");
		try {
			while (rs.next()) {
				status = rs.getString("status");				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBConnector.CloseAll();
		return status;
	}
	
	@Override
	public String GetProductStatus(String id) {
		String status = "no status found";
		ResultSet rs = DBConnector
				.GetResultSet("SELECT status FROM product WHERE id ='"
						+ id + "'");
		try {
			while (rs.next()) {
				status = rs.getString("status");				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBConnector.CloseAll();
		return status;
	}

	@Override
	public void ChangeProductStatus(String id, String newstatus) {
		DBConnector.SendStatement("UPDATE product SET status='" + newstatus
				+ "'WHERE id='" + id + "'");
		DBConnector.CloseAll();		
	}
	
	@Override
	public void ChangeUserType(String email, String newtype) {
		DBConnector.SendStatement("UPDATE users SET usertype='" + newtype
				+ "'WHERE email='" + email + "'");
		DBConnector.CloseAll();
	}
	
	@Override
	public String GetUserType(String email) {
		String status = "fake status";
		ResultSet rs = DBConnector
				.GetResultSet("SELECT usertype FROM users WHERE email ='"
						+ email + "'");
		try {
			while (rs.next()) {
				status = rs.getString("usertype");				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBConnector.CloseAll();
		return status;
	}

	@Override
	public void DeleteUser(String id) {
		DBConnector.SendStatement("DELETE FROM users WHERE id='" + id + "'");
		DBConnector.CloseAll();
	}
	@Override
	public void DeleteProduct(String id) {
		DBConnector.SendStatement("DELETE FROM product WHERE id='" + id + "'");
		DBConnector.CloseAll();		
	}

	@Override
	public List<User> FindMultiUser(String keyword) {
		ResultSet rs = DBConnector
				.GetResultSet("SELECT * FROM users WHERE name LIKE '%"+keyword+"%'OR lastname LIKE '%"+keyword+"%' OR id LIKE '%"+keyword+"%' " +
						"OR email LIKE '%"+keyword+"%'OR usertype LIKE '%"+keyword+"%' OR status LIKE '%"+keyword+"%'");
		List<User> userList = null;
		userList = GetUserObject.GetMultiUser(rs);
		try {
			rs.close();
			DBConnector.CloseAll();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return userList;		
	}

	@Override
	public List<Product> FindMultiProduct(String keyword) {
		ResultSet rs = DBConnector
				.GetResultSet("SELECT * FROM product WHERE name LIKE '%"+keyword+"%'OR minimumprice LIKE '%"+keyword+"%' OR id LIKE '%"+keyword+"%' " +
						"OR description LIKE '%"+keyword+"%'");
		List<Product> productList = null;
		productList = GetProductObject.GetMultiProduct(rs);	
		try {
			rs.close();
			DBConnector.CloseAll();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return productList;	
		
	}
	
	public User FindSingleUser(String email) {
		ResultSet rs = DBConnector
				.GetResultSet("SELECT * FROM users WHERE email= '"+email+"'");
		User user = null;
		user = GetUserObject.GetSingleUser(rs);
		try {
			rs.close();
			DBConnector.CloseAll();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;		
	}

	public Product FindSingleProduct(String id) {
		ResultSet rs = DBConnector
				.GetResultSet("SELECT * FROM product WHERE id = '"+id+"' ");
		Product product = null;
		product = GetProductObject.GetSingleProduct(rs);		
		try {
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBConnector.CloseAll();
		return product;			
	}

	@Override
	public void UpdateUser(String oldemail, String name, String lastname, String email, String credits) {
		int cred = Integer.valueOf(credits);
		DBConnector.SendStatement("UPDATE users SET name='" + name
				+ "'WHERE email='" + oldemail + "'");
		DBConnector.SendStatement("UPDATE users SET lastname='" + lastname
				+ "'WHERE email='" + oldemail + "'");
		DBConnector.SendStatement("UPDATE users SET email='" + email
				+ "'WHERE email='" + oldemail + "'");
		DBConnector.SendStatement("UPDATE users SET credits='" + cred
				+ "'WHERE email='" + oldemail + "'");
		DBConnector.CloseAll();		
	}

	@Override
	public void UpdateProduct(int id, String name, int price,
			String description) {
		DBConnector.SendStatement("UPDATE product SET name='" + name
				+ "'WHERE id='" + id + "'");/*
		db.SendStatement("UPDATE product SET price='" + price
				+ "'WHERE id='" + id + "'");
		db.SendStatement("UPDATE product SET description='" + description
				+ "'WHERE id='" + id + "'");		*/
		DBConnector.CloseAll();		
	}
}